import { reqAllGoods, reqGroup, reqNew, reqSeckil } from "../../http/api";
import { createSelector } from 'reselect'
// 1.初始值
const initState = {
    // 1.1 初始化
    banner: [],
    // 2.万人团初始值
    group: [],
    //3.1 秒杀初始值
    seckil: [],
    // 4.1 初始化商品列表
    allGoods: [],
    // 5.1 初始化n
    n: 0
}
// types
const types = {
    // 1.2 修改banner type 
    HOME_CHANGE_BANNER: "HOME_CHANGE_BANNER",
    // 2.2修改了万人团
    HOME_CHANGE_GROUP: "HOME_CHANGE_GROUP",
    // 3.2 修改了秒杀活动
    HOME_CHANGE_SECKIL: "HOME_CHANGE_SECKIL",
    // 4.2 修改了所有商品
    HOME_CHANGE_ALL_GOODS: "HOME_CHANGE_ALL_GOODS",
    // 5.2 修改了n
    HOME_CHANGE_N: "HOME_CHANGE_N"

}


// reducer
export const homeReducer = (state = initState, action) => {
    switch (action.type) {
        //1.3修改banner reducer
        case types.HOME_CHANGE_BANNER:
            // {type:types.HOME_CHANGE_BANNER,banner:[{}]}
            return {
                ...state,
                banner: action.banner
            }
            break;
        // 2.3修改了万人团的数据
        case types.HOME_CHANGE_GROUP:
            return {
                ...state,
                group: action.group
            }
            break;
        // 3.3修改了秒杀活动的数据
        case types.HOME_CHANGE_SECKIL:
            return {
                ...state,
                seckil: action.seckil
            }
            break;
        // 4.3 修改了所有商品的数据
        case types.HOME_CHANGE_ALL_GOODS:
            return {
                ...state,
                allGoods: action.allGoods
            }
            break;
        // 5.3 修改了n
        case types.HOME_CHANGE_N:
            return {
                ...state,
                n: action.n
            }
            break;
        default:
            return state;
    }
}

// actions
export const actions = {
    // 1.4 修改banner  action 
    changeBanner: banner => ({ type: types.HOME_CHANGE_BANNER, banner }),
    // 2.4修改group--万人团
    changeGroup: group => ({ type: types.HOME_CHANGE_GROUP, group }),
    // 3.4 修改了seckil---秒杀活动
    changeSeckil: seckil => ({ type: types.HOME_CHANGE_SECKIL, seckil }),
    // 4.4 修改了所有商品
    changeAllGoods: allGoods => ({ type: types.HOME_CHANGE_ALL_GOODS, allGoods }),
    // 5.4 修改了n
    changeN: n => ({ type: types.HOME_CHANGE_N, n }),
    //1.6发请求 做异步，actions里面不能return{},只能return函数
    reqBannerAction: () => {
        return (dispatch, getState) => {
            // 1.9 将状态层作为缓存层
            let { banner } = getState().home
            if (banner.length > 0) {
                return;
            }
            //1.7 发请求
            reqNew().then(res => {
                if (res.data.code === 200) {
                    //1.8 修改banner
                    dispatch(actions.changeBanner(res.data.list))
                }
            })
        }
    },
    // 2.6 发请求，做异步----return 可省略
    reqGroupAction: () => (dispatch, getState) => {
        // 2.9 将状态层作为缓存层
        let { group } = getState().home
        if (group.length > 0) {
            return;
        }
        // 2.7 发请求
        reqGroup().then(res => {
            if (res.data.code === 200) {
                // 2.8 修改group
                dispatch(actions.changeGroup(res.data.list))
            }

        })
    },
    // 3.6 发请求，做异步
    reqSeckilAction: () => {
        return (dispatch, getState) => {
            // 3.9 将状态层作为缓存层
            let { seckil } = getState().home
            if (seckil.length > 0) {
                return;
            }
            //3.7  发请求
            reqSeckil().then(res => {
                //3.8 修改seckil 
                if (res.data.code === 200) {
                    dispatch(actions.changeSeckil(res.data.list))
                }
            })
        }
    },
    // 4.6 发请求，做异步
    reqAllGoodsAction: () => {
        return (dispatch, getState) => {
            // 4.9 将状态层作为缓存层
            let { allGoods } = getState().home
            if (allGoods.length > 0) {
                return;
            }
            // 4.7 发请求
            reqAllGoods().then(res => {
                // 4.8 修改allGoods
                if (res.data.code === 200) {
                    dispatch(actions.changeAllGoods(res.data.list))
                }
            })
        }
    }
}

// 导出数据
// 1.5导出banner
export const getBanner = state => state.home.banner
// 2.5 导出group
export const getGroup = state => state.home.group
// 3.5 导出seckil
export const getSeckil = state => state.home.seckil
// 4.5 导出allGoods
export const getAllGoods = state => state.home.allGoods
// 5.5 导出n
export const getN = state => state.home.n
// 5.6 因为要根据n和allGoods计算出最终在页面中展示的数据，所以要将最终展示的数据导出

// createSelector([以来的数据],()=>计算的结果)
export const getShowList = createSelector([getAllGoods, getN], (allGoods, n) => {
    console.log("===========计算了计算了计算了,只需要计算一次，所以要用到getState==============");
    return allGoods.length > 0 ? allGoods[n].content : []
})

/* state => {
    console.log("===========计算了计算了计算了,只需要计算一次，所以要用到getState==============");
    let { allGoods, n } = state.home
    // 如果直接展示，allGoods请求回来之前是一个空数组，会报错，所以要做一个判断
    return allGoods.length > 0 ? allGoods[n].content : []
} */


